package com.qo.qsdk.ui.base.floater;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.PixelFormat;
import android.graphics.Point;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.LinearLayout;


import com.qo.qsdk.ui.base.floater.animation.AlphaValueAnimation;
import com.qo.qsdk.ui.base.floater.animation.AssistorViewMoveToSideAnimation;
import com.qo.qsdk.ui.base.floater.ctrl.AssistorView;
import com.qo.qsdk.ui.base.floater.parameters.AssistorViewParameters;
import com.qo.qsdk.utils.DynamicUtils;
import com.qo.qsdk.utils.QSDKHelper;

import java.util.Timer;
import java.util.TimerTask;

public class Assistor {

    private static final String TAG = Assistor.class.getSimpleName();

    private static final int MAGNETIC_THRESHOLD = 10;
    private static final int ASSISTOR_VIEW_MOVE_TO_SIDE_ANIMATION_MILLISECONDS = 8;
    private static final int ASSISTOR_VIEW_ALPHA_VALUE_ANIMATION_MILLISECONDS = 8;

    private Activity mParentActivity;
    private LinearLayout mAssistorViewFlashLinearLayout;
    private ImageButton mAssistorViewFlashImageButton;
    private AnimationSet mAssistorViewFlashImageButtonAnimationSet;

    private boolean mAssistorToolbarDismiss;
    private long mCurrentTimeMillis;
    private float[] mAssistorViewLocation;

    private WindowManager mWindowManager;
    private WindowManager.LayoutParams mAssistorLayoutParams;
    private AssistorView mAssistorView;
    private FrameLayout mAssistorViewFrameLayout;
    private AssistorViewParameters mAssistorViewParameters;

    private int mMoveThreshold;
    private Point mPoint;
    private String mKeySettingsViewLocationX;
    private String mKeySettingsViewLocationY;

    private Timer mAssistorViewMoveToSideAnimationTimer;
    private Timer mAssistorViewAlphaValueAnimationTimer;

    private boolean mAlwaysIndicateAssistor;

    private int mAssistorViewWidth;
    private int mAssistorViewHeight;
    private boolean mIsAssistorViewVisible;
    private boolean mIsAssistorViewPlaceRight;

    private AlphaValueAnimation mAssistorViewAlphaValueAnimation;
    private AssistorViewMoveToSideAnimation mAssistorViewMoveToSideAnimation;

    private int mDisplayMetricsWidthPixels;
    private int mDisplayMetricsHeightPixels;
    private int mStatusBarHeight;
    private boolean mAssistorViewIsMove;
    private int mSaveAssistorLayoutParamsX;
    private int mSaveAssistorLayoutParamsY;

    private View.OnTouchListener mAssistorViewOnTouchListener;

    public Assistor() {
        super();

        mAssistorToolbarDismiss = true;
        mCurrentTimeMillis = 0L;
        mAssistorViewIsMove = false;
        mPoint = new Point();
        mAssistorViewMoveToSideAnimationTimer = null;
        mAssistorViewAlphaValueAnimationTimer = null;
        mAssistorViewLocation = new float[] { 0.0f, 0.0f };
        mAlwaysIndicateAssistor = false;

        mAssistorViewOnTouchListener = new AssistorViewOnTouchListener();

    }

    public void initialize(Activity parentActivity) {
        mParentActivity = parentActivity;

        assert parentActivity != null;
        mWindowManager = (WindowManager)parentActivity.getSystemService(Context.WINDOW_SERVICE);

        mAssistorLayoutParams = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_APPLICATION,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT);
        mAssistorLayoutParams.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
        mAssistorLayoutParams.gravity = Gravity.TOP | Gravity.LEFT;

        mAssistorViewParameters = new AssistorViewParameters();
        mAssistorViewParameters.setDefaultIcon("qsdk_float_normal");
        mAssistorViewParameters.setNormalIcon("qsdk_float_normal");
        mAssistorViewParameters.setExpandedIcon("qsdk_float_normal");
        mAssistorViewParameters.setWidth(AssistorViewParameters.WIDTH);
        mAssistorViewParameters.setHeight(AssistorViewParameters.HEIGHT);
        mAssistorViewParameters.setStartX(AssistorViewParameters.START_X);
        mAssistorViewParameters.setStartY(AssistorViewParameters.START_Y);
        mAssistorViewParameters.setMoveThreshold(AssistorViewParameters.MOVE_THRESHOLD);
        mAssistorViewParameters.setSlideIconRightA("qsdk_float_normal");
        mAssistorViewParameters.setSlideIconRightB("qsdk_float_normal");
        mAssistorViewParameters.setSlideIconLeftA("qsdk_float_normal");
        mAssistorViewParameters.setSlideIconLeftB("qsdk_float_normal");
        mAssistorViewParameters.setFloatButtonFlashImage("qsdk_float_normal");

        mMoveThreshold = QSDKHelper.dip2px(mParentActivity, mAssistorViewParameters.getMoveThreshold());

        initAssistorView();
    }

    public void showAssistorView(Activity activity, final double percentageX, final double percentageY,
                                 final boolean isShow) {
        if (isShow) {
            if (mIsAssistorViewVisible) {

                cancelAssistorViewMoveToSideAnimationTimer();
                cancelAssistorViewAlphaValueAnimationTimer();

                final WindowManager.LayoutParams assistorViewLayoutParams = initAssistorViewLayoutParams(percentageX, percentageY);
                mWindowManager.updateViewLayout(mAssistorViewFlashLinearLayout, assistorViewLayoutParams);
                mWindowManager.updateViewLayout(mAssistorViewFrameLayout, assistorViewLayoutParams);
                return;
            }

            if (mAssistorViewFrameLayout.getVisibility() == View.GONE) {
                mAssistorViewFrameLayout.setVisibility(View.VISIBLE);
                mAssistorViewAlphaValueAnimation.clearAnimation();
                mAssistorViewMoveToSideAnimation.clearMoveToSideAnimation();
                mAssistorView.setNormalBackgroundDrawable(activity, mAssistorViewParameters.getNormalIcon());

                int isEdge;
                final int x = mAssistorLayoutParams.x;
                if (mParentActivity.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
                    isEdge = 1;
                } else {
                    final DisplayMetrics displayMetrics = mParentActivity.getResources().getDisplayMetrics();
                    mDisplayMetricsWidthPixels = displayMetrics.widthPixels;
                    mDisplayMetricsHeightPixels = displayMetrics.heightPixels;
                    if (x <= MAGNETIC_THRESHOLD || x >= mDisplayMetricsWidthPixels - MAGNETIC_THRESHOLD - mAssistorViewWidth) {
                        isEdge = 1;
                    } else {
                        isEdge = 0;
                    }
                }

                mIsAssistorViewPlaceRight = x >= mDisplayMetricsWidthPixels / 2 - mAssistorViewWidth / 2;

                if (isEdge != 0) {
                    if (!mAlwaysIndicateAssistor) {
                        scheduleAssistorViewMoveToSideAnimationTimer();
                    }
                } else if (!mAlwaysIndicateAssistor) {
                    scheduleAssistorViewAlphaValueAnimationTimer();
                }
            }
            else {
                final WindowManager.LayoutParams assistorViewLayoutParams = initAssistorViewLayoutParams(percentageX, percentageY);
                mWindowManager.addView(mAssistorViewFlashLinearLayout, assistorViewLayoutParams);
                mWindowManager.addView(mAssistorViewFrameLayout, assistorViewLayoutParams);
            }
            mIsAssistorViewVisible = true;

        } else if (mIsAssistorViewVisible) {
            mAssistorViewFlashImageButton.setVisibility(View.INVISIBLE);
            mAssistorViewFlashLinearLayout.setVisibility(View.INVISIBLE);
            setShrinkAssistor();
            mAssistorViewFrameLayout.setVisibility(View.GONE);
            mAssistorViewFlashImageButton.setVisibility(View.INVISIBLE);
            mAssistorViewFlashLinearLayout.setVisibility(View.INVISIBLE);
            mIsAssistorViewVisible = false;
        }
    }

    public void destroyAssistorView() {
        synchronized (this) {
            if (Looper.myLooper() == Looper.getMainLooper()) {
                insideDestroyAssistorView();
            } else {
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    public void run() {
                        insideDestroyAssistorView();
                    }
                });
            }
        }
    }

    public boolean isAssistorViewVisible() {
        return mIsAssistorViewVisible;
    }

    private void insideDestroyAssistorView() {
        if (mIsAssistorViewVisible ||
                mAssistorViewFrameLayout.getVisibility() == View.GONE) {

            mWindowManager.removeViewImmediate(mAssistorViewFlashLinearLayout);
            mWindowManager.removeViewImmediate(mAssistorViewFrameLayout);
            mAssistorViewFlashLinearLayout = null;
            mAssistorViewFrameLayout = null;
            mIsAssistorViewVisible = false;
        }
    }

    @TargetApi(11)
    private void setLayerType(View view) {
        if (Build.VERSION.SDK_INT >= 11) {
            try {
                if (view.isHardwareAccelerated()) {
                    view.setLayerType(1, null);
                }
            } catch (Exception e) {
                Log.e(TAG, "#* EE: disable gpu exception");
            }
        }
    }

    @SuppressLint({"ClickableViewAccessibility"})
    private void initAssistorView() {

        mAssistorViewWidth = QSDKHelper.dip2px(mParentActivity, mAssistorViewParameters.getWidth());
        mAssistorViewHeight = QSDKHelper.dip2px(mParentActivity, mAssistorViewParameters.getHeight());

        ViewGroup.LayoutParams assistorViewFrameLayoutLayoutParams =
                new FrameLayout.LayoutParams(mAssistorViewWidth, mAssistorViewHeight);
        ((FrameLayout.LayoutParams)(assistorViewFrameLayoutLayoutParams)).gravity = Gravity.CENTER;

        ViewGroup.LayoutParams layoutParams2 = new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT);

        mAssistorViewFrameLayout = new FrameLayout(mParentActivity);
        mAssistorViewFrameLayout.setLayoutParams(layoutParams2);
        mAssistorView = new AssistorView(mParentActivity, mAssistorViewParameters);
        mAssistorView.setLayoutParams(assistorViewFrameLayoutLayoutParams);
        mAssistorView.setOnTouchListener(mAssistorViewOnTouchListener);
        mAssistorViewFrameLayout.addView(mAssistorView);

        mAssistorViewAlphaValueAnimation = new AlphaValueAnimation(mAssistorView, 0.2f, 800);
        mAssistorViewFlashLinearLayout = new LinearLayout(mParentActivity);
        mAssistorViewFlashLinearLayout.setLayoutParams(layoutParams2);
        mAssistorViewFlashImageButton = new ImageButton(mParentActivity);
        mAssistorViewFlashImageButton.setLayoutParams(assistorViewFrameLayoutLayoutParams);
        mAssistorViewFlashImageButton.setBackgroundDrawable(DynamicUtils.getDrawable(mParentActivity, mAssistorViewParameters.getFloatButtonFlashImage()));

        mAssistorViewFlashLinearLayout.addView(mAssistorViewFlashImageButton);

        mAssistorViewFlashImageButton.setVisibility(View.INVISIBLE);
        mAssistorViewFlashLinearLayout.setVisibility(View.INVISIBLE);

        mAssistorViewFlashImageButtonAnimationSet = new AnimationSet(true);
        {
            Animation animation = new AlphaAnimation(0.1f, 0.5f);
            animation.setDuration(200);
            animation.setStartOffset(0);
            mAssistorViewFlashImageButtonAnimationSet.addAnimation(animation);
        }

        {
            Animation animation = new ScaleAnimation(0.6f, 1.0f, 0.6f, 1.0f, 1, 0.5f, 1, 0.5f);
            animation.setDuration(200);
            animation.setStartOffset(0);
            mAssistorViewFlashImageButtonAnimationSet.addAnimation(animation);
        }

        {
            Animation animation = new AlphaAnimation(0.5f, 0.0f);
            animation.setDuration(10);
            animation.setStartOffset(200);
            mAssistorViewFlashImageButtonAnimationSet.addAnimation(animation);
        }

        mAssistorViewFlashImageButtonAnimationSet.setFillAfter(true);

        setLayerType(mAssistorViewFlashLinearLayout);
        setLayerType(mAssistorViewFrameLayout);

        if (QSDKHelper.isActivityFullScreen(mParentActivity)) {
            mStatusBarHeight = 0;
        } else {
            mStatusBarHeight = QSDKHelper.statusBarHeight(mParentActivity);
        }
        mAssistorViewMoveToSideAnimation = new AssistorViewMoveToSideAnimation(
                mAssistorView,
                mAssistorViewParameters,
                false);
    }

    private void cancelAssistorViewMoveToSideAnimationTimer() {
        synchronized (this) {
            if (mAssistorViewMoveToSideAnimationTimer != null) {
                mAssistorViewMoveToSideAnimationTimer.cancel();
                mAssistorViewMoveToSideAnimationTimer = null;
            }
        }
    }

    private void cancelAssistorViewAlphaValueAnimationTimer() {
        synchronized (this) {
            if (mAssistorViewAlphaValueAnimationTimer != null) {
                mAssistorViewAlphaValueAnimationTimer.cancel();
                mAssistorViewAlphaValueAnimationTimer = null;
            }
        }
    }

    private WindowManager.LayoutParams initAssistorViewLayoutParams(final double xPercentage, final double yPercentage) {
        mAssistorView.setNormalBackgroundDrawable();

        final DisplayMetrics displayMetrics = mParentActivity.getResources().getDisplayMetrics();
        mDisplayMetricsWidthPixels = displayMetrics.widthPixels;
        mDisplayMetricsHeightPixels = displayMetrics.heightPixels;

        if (mParentActivity.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
            mKeySettingsViewLocationX = mParentActivity.getClass().getName() + "_landscape_x";
            mKeySettingsViewLocationY = mParentActivity.getClass().getName() + "_landscape_y";
        }
        else if (mParentActivity.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
            mKeySettingsViewLocationX = mParentActivity.getClass().getName() + "_portrait_x";
            mKeySettingsViewLocationY = mParentActivity.getClass().getName() + "_portrait_y";
        }

        int pixelsX;
        if (xPercentage >= 0.0 && xPercentage <= 100.0) {
            if (xPercentage >= 50.0) {
                pixelsX = mDisplayMetricsWidthPixels;
            }
            else {
                pixelsX = 0;
            }
        }
        else if (mAssistorViewParameters.getStartX() > 50.0) {
            pixelsX = mDisplayMetricsWidthPixels;
        }
        else {
            pixelsX = 0;
        }

        int pixelsY;
        if (yPercentage >= 0.0 && yPercentage <= 100.0) {
            pixelsY = (int)((mDisplayMetricsHeightPixels - mAssistorViewHeight) * yPercentage) / 100;
        }
        else {
            pixelsY = (int)((mDisplayMetricsHeightPixels - mAssistorViewHeight) * mAssistorViewParameters.getStartY()) / 100;
        }

        mAssistorLayoutParams.x = AssistorPreference.get(mParentActivity, mKeySettingsViewLocationX, pixelsX);
        mAssistorLayoutParams.y = AssistorPreference.get(mParentActivity, mKeySettingsViewLocationY, pixelsY);

        final int x = mAssistorLayoutParams.x;
        mIsAssistorViewPlaceRight = x >= mDisplayMetricsWidthPixels / 2 - mAssistorViewWidth / 2;

        if (x <= MAGNETIC_THRESHOLD || x >= mDisplayMetricsWidthPixels - MAGNETIC_THRESHOLD - mAssistorViewWidth) {
            if (!mAlwaysIndicateAssistor) {
                scheduleAssistorViewMoveToSideAnimationTimer();
            }
        } else if (!mAlwaysIndicateAssistor) {
            scheduleAssistorViewAlphaValueAnimationTimer();
        }

        return mAssistorLayoutParams;
    }

    private void saveAssistorViewLocation() {
        AssistorPreference.set(mParentActivity, mKeySettingsViewLocationX, mPoint.x);
        AssistorPreference.set(mParentActivity, mKeySettingsViewLocationY, mPoint.y);
    }

    private void scheduleAssistorViewMoveToSideAnimationTimer() {
        cancelAssistorViewMoveToSideAnimationTimer();
        (mAssistorViewMoveToSideAnimationTimer = new Timer()).schedule(new TimerTask() {
            @Override
            public void run() {
                if (mAssistorViewMoveToSideAnimation != null) {
                    QSDKHelper.getHandler().post((Runnable) new Runnable() {
                        @Override
                        public void run() {
                            if (!mAssistorToolbarDismiss) {
                                closeToolbar();
                            }
                            mAssistorViewMoveToSideAnimation.startMoveToSideAnimation(mParentActivity,mIsAssistorViewPlaceRight);
                        }
                    });
                }
            }
        }, ASSISTOR_VIEW_MOVE_TO_SIDE_ANIMATION_MILLISECONDS * 1000);
    }

    private void scheduleAssistorViewAlphaValueAnimationTimer() {
        this.cancelAssistorViewAlphaValueAnimationTimer();
        (mAssistorViewAlphaValueAnimationTimer = new Timer()).schedule(new TimerTask() {
            @Override
            public void run() {
                QSDKHelper.getHandler().post((Runnable) new Runnable() {
                    @Override
                    public void run() {
                        mAssistorViewAlphaValueAnimation.startAnimation();
                        if (!mAssistorToolbarDismiss) {
                            closeToolbar();
                        }
                    }
                });
            }
        }, ASSISTOR_VIEW_ALPHA_VALUE_ANIMATION_MILLISECONDS * 1000);
    }

    private void setShrinkAssistor() {
        mAlwaysIndicateAssistor = false;
    }

    private void openToolbar(final boolean right) {
        mCurrentTimeMillis = System.currentTimeMillis();
        mAssistorToolbarDismiss = false;
        mAssistorViewFlashLinearLayout.setVisibility(View.VISIBLE);
        mAssistorViewFlashImageButton.setVisibility(View.VISIBLE);
        mAssistorViewFlashImageButton.startAnimation(mAssistorViewFlashImageButtonAnimationSet);

        // action open
//        FloaterActivity.startActivity(mParentActivity);
        closeToolbar();
    }

    private void closeToolbar() {
        mCurrentTimeMillis = System.currentTimeMillis();
        mAssistorToolbarDismiss = true;
    }

    private class AssistorViewOnTouchListener implements View.OnTouchListener {

        @Override
        public boolean onTouch(final View view, final MotionEvent event) {
            final int action = event.getAction();
//            if (!mAssistorViewIsMove) {
//                mSaveAssistorLayoutParamsX = mAssistorLayoutParams.x;
//                mSaveAssistorLayoutParamsY = mAssistorLayoutParams.y;
//            }

            final float xInScreen = event.getRawX();
            final float yInScreen = event.getRawY() - mStatusBarHeight;

            final DisplayMetrics displayMetrics = mParentActivity.getResources().getDisplayMetrics();
            mDisplayMetricsWidthPixels = displayMetrics.widthPixels;
            mDisplayMetricsHeightPixels = QSDKHelper.getRealHight(mParentActivity);// displayMetrics.heightPixels;

            cancelAssistorViewMoveToSideAnimationTimer();
            cancelAssistorViewAlphaValueAnimationTimer();

            switch (action) {
                case MotionEvent.ACTION_DOWN: {
                    mAssistorViewAlphaValueAnimation.clearAnimation();
                    mAssistorViewMoveToSideAnimation.clearMoveToSideAnimation();
                    mAssistorView.setNormalBackgroundDrawable(mParentActivity, mAssistorViewParameters.getNormalIcon());

                    mAssistorViewLocation[0] = event.getX();
                    mAssistorViewLocation[1] = event.getY();

                    mSaveAssistorLayoutParamsX = (int) (xInScreen - mAssistorViewLocation[0]);
                    mSaveAssistorLayoutParamsY = (int) (yInScreen - mAssistorViewLocation[1]);

                    return false;
                }
                case MotionEvent.ACTION_MOVE: {
                    mAssistorLayoutParams.x = (int) (xInScreen - mAssistorViewLocation[0]);
                    mAssistorLayoutParams.y = (int) (yInScreen - mAssistorViewLocation[1]);

                    if (mAssistorViewIsMove) {
                        mWindowManager.updateViewLayout(mAssistorViewFrameLayout, mAssistorLayoutParams);
                        return false;
                    }
                    if (Math.abs(mAssistorLayoutParams.x - mSaveAssistorLayoutParamsX) > mMoveThreshold ||
                            Math.abs(mAssistorLayoutParams.y - mSaveAssistorLayoutParamsY) > mMoveThreshold) {
                        mAssistorViewIsMove = true;
                        mAssistorToolbarDismiss = true;

                        mWindowManager.updateViewLayout(mAssistorViewFrameLayout, mAssistorLayoutParams);
                        return false;
                    }
                    break;
                }
                case MotionEvent.ACTION_UP: {
                    final int xCurrent = mAssistorLayoutParams.x;
                    boolean isEdge;
                    if (mParentActivity.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
                        if (xCurrent <= MAGNETIC_THRESHOLD || xCurrent >= mDisplayMetricsWidthPixels - MAGNETIC_THRESHOLD - mAssistorViewWidth) {
                            isEdge = true;
                        } else {
                            isEdge = false;
                        }
                        isEdge = true;
                    } else if (mParentActivity.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
                        isEdge = true;
                    } else {
                        isEdge = true;
                    }

                    if (mAssistorViewIsMove) {
                        if (isEdge) {
                            if (xCurrent >= mDisplayMetricsWidthPixels / 2 - mAssistorViewWidth / 2) {
                                mAssistorLayoutParams.x = mDisplayMetricsWidthPixels;
                                mAssistorLayoutParams.y = (int)(yInScreen - mAssistorViewLocation[1]);
                                mIsAssistorViewPlaceRight = true;
                            }
                            else {
                                mAssistorLayoutParams.x = 0;
                                mAssistorLayoutParams.y = (int)(yInScreen - mAssistorViewLocation[1]);
                                mIsAssistorViewPlaceRight = false;
                            }

                            mWindowManager.updateViewLayout(mAssistorViewFlashLinearLayout, mAssistorLayoutParams);
                            mWindowManager.updateViewLayout(mAssistorViewFrameLayout, mAssistorLayoutParams);
                            if (!mAlwaysIndicateAssistor) {
                                scheduleAssistorViewMoveToSideAnimationTimer();
                            }
                        }
                        else {
                            mAssistorLayoutParams.x = (int)(xInScreen - mAssistorViewLocation[0]);
                            mAssistorLayoutParams.y = (int)(yInScreen - mAssistorViewLocation[1]);
                            mWindowManager.updateViewLayout(mAssistorViewFlashLinearLayout, mAssistorLayoutParams);
                            mWindowManager.updateViewLayout(mAssistorViewFrameLayout, mAssistorLayoutParams);
                            if (!mAlwaysIndicateAssistor) {
                                scheduleAssistorViewAlphaValueAnimationTimer();
                            }
                        }
                        mPoint.set(mAssistorLayoutParams.x, mAssistorLayoutParams.y);
                        saveAssistorViewLocation();
                        mAssistorViewIsMove = false;
                        break;
                    } else {
                        setShrinkAssistor();
                        if (System.currentTimeMillis() - mCurrentTimeMillis >= 200L) {
                            if (xCurrent >= mDisplayMetricsWidthPixels / 2 - mAssistorViewWidth / 2) {
                                if (mAssistorToolbarDismiss) {
                                    // <-- AV
                                    openToolbar(false);
                                } else {
                                    // close
                                    closeToolbar();
                                }
                                mIsAssistorViewPlaceRight = true;
                            } else {
                                if (mAssistorToolbarDismiss) {
                                    // AV -->
                                    openToolbar(true);
                                }
                                else {
                                    // close
                                    closeToolbar();
                                }
                                mIsAssistorViewPlaceRight = false;
                            }
                        }

                        if (isEdge) {
                            scheduleAssistorViewMoveToSideAnimationTimer();
                            return false;
                        }

                        scheduleAssistorViewAlphaValueAnimationTimer();
                        return false;
                    }
                }
            }

            return false;
        }
    };


}
